<template>
  <el-row :gutter="20">
    <el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6" v-for="item in filterItems" :key="item.id">
      <div class="filter">
        <span>{{item.title}}:</span>
        <el-input
          v-if="item.type==='input'"
          size="mini"
          v-model="filterParam[item.id]"
          :placeholder="item.placeholder||'请输入内容'"
          @keyup.enter.native="searchData"
        ></el-input>
        <el-date-picker
          v-if="item.type==='date'"
          :editable="false"
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="filterParam[item.id]"
          type="datetimerange"
          size="mini"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
    </el-col>
    <el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
      <div class="search-btn">
        <el-button size="mini" type="primary" icon="el-icon-search" @click="searchData">查询</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetData">重置</el-button>
      </div>
    </el-col>
  </el-row>
</template>
<script>
import moment from "moment";
export default {
  // filterprops{
  //    filterItems=>Array
  //    filterChange=>func
  // }
  props: {
    filterProps: Object
  },
  data() {
    let { filterItems, onFilterChange, reSet } = this.filterProps;
    let filterParam = {};
    filterItems.forEach(item => {
      filterParam[`${item.id}`] = "";
    });
    console.log(filterParam);
    return {
      filterItems,
      filterParam,
      onFilterChange,
      reSet
    };
  },
  methods: {
    searchData() {
      let keysArr = [];
      for (let key in this.filterParam) {
        if (typeof this.filterParam[key] === "string") {
          if (this.filterParam[key].replace(/\s+/g, "") === "") {
            delete this.filterParam[key];
          }
        }
      }
      this.onFilterChange(this.filterParam);
    },
    resetData() {
      for (let key in this.filterParam) {
        this.filterParam[key] = "";
      }
      this.reSet();
    }
  }
};
</script>
<style lang='scss' scoped>
.el-row {
  margin: -20px 0 20px;
  .filter {
    display: flex;
    font-size: 12px;
    span {
      white-space: nowrap;
      display: flex;
      align-items: center;
      margin-right: 5px;
    }
  }
  .search-btn {
    display: flex;
  }
  .el-col {
    margin-top: 20px;
  }
}
</style>